# -*- coding: utf-8 -*-

# File Name： u_info
# Description :
# Author : lirui
# create_date： 2022/9/15
# Change Activity:
from modules.generate_label.obj.board_labels_generator import BoardLabelsGenerator


class UInfo:
    def __init__(self, u_info_lst):
        """
        cable in fo in one U.
        Args:
            u_info_lst:
        """
        self.u_info_lst = u_info_lst
        self.is_rj45 = self._check_is_rj45()
        self.u_label = self._generate_u_label()
        if not self.is_rj45:
            self.board_label_lst = self._generate_board_label_lst(u_info_lst)  # generate all labels in one u.

    @staticmethod
    def _generate_board_label_lst(u_info_lst):
        """
        Collect board labels list.
        Args:
            u_info_lst:

        Returns:

        """
        label_text_lst = []
        for info in u_info_lst:
            generator = BoardLabelsGenerator(info)
            label_text_lst = label_text_lst + generator.labels_text_lst
        return label_text_lst

    def _gen_rj45_u_label(self):
        """

        Returns:

        """
        dct = self.u_info_lst[0]
        label_text = rf"""本端：{dct['本端地址']}.{dct['本端U数']} 对端：{dct['对端地址']}.{dct['对端U数']}"""
        return label_text

    def _generate_u_label(self):
        """

        Returns:

        """
        if self.is_rj45:
            return [self._gen_rj45_u_label()]
        else:
            label_text_lst = []
            for cable_info in self.u_info_lst:
                label_text = self._gen_optical_cable(cable_info)
                label_text_lst.append(label_text)
            return label_text_lst

    @staticmethod
    def _gen_optical_cable(cable_info):

        """

        Args:
            cable_info:

        Returns:

        """
        dct = cable_info
        label_text = f"""本端：{dct['本端地址']}.{dct['本端U数']} {dct['线缆类型']}\n对端：{dct['对端地址']}.{dct['对端U数']}"""
        return label_text

    def _check_is_rj45(self):
        """

        Returns:

        """
        if 'CAT6' in self.u_info_lst[0]['线缆类型'].upper():
            return True
        return False
